// pages/cart/cart.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    "carts": [{
      "id": "102",
      "num": "4",
      "photo_little": "http://www.onajax.com/Data/UploadFiles/product/20190623/1561281269462133.jpg",
      "pid": "8",
      "price": "5488.00",
      "pro_name": "【徕卡四摄 50倍数码变焦】Huawei/华为P30 Pro曲面屏超感光徕卡四摄变焦980芯片智能手"
    },
      {
        "uid": "5",
        "id": "103",
        "num": "1",
        "photo_little": "http://www.onajax.com/Data/UploadFiles/product/20190623/1561281269462133.jpg",
        "pid": "6",
        "price": "9999.00",
        "pro_name": "【6期免息】【买赠Freebuds2 Pro】Huawei/华为 Mate 20 RS 保时捷设计超",
        "uid": "5"
      },
      {
        "id": "104",
        "num": "1",
        "photo_little": "http://www.onajax.com/Data/UploadFiles/product/20190623/1561281269462133.jpg",
        "pid": "324",
        "price": "6188.00",
        "pro_name": "【下单立减800元】Apple/苹果 MacBook Air 13.3英寸128G 笔记本手提电脑 ",
        "uid": "5"
      }
    ],
    total:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      carts: this.data.carts
    })
  },

  bindCheckbox:function(e) {
    var _index = parseInt(e.currentTarget.dataset.index)
    var _selected = this.data.carts[_index].selected
    var carts = this.data.carts
    carts[_index].selected = !_selected

    this.setData({
      carts: carts
    })

    this.sum()
  },

  sum: function(e) {
    console.log("执行开始")
    var _carts = this.data.carts
    var _total = 0
    for (var i = 0;i<_carts.length;i++) {
      if(_carts[i].selected) {
        _total += _carts[i].num * _carts[i].price
      }
    }

    console.log(_total)

    this.setData({
      total: _total,
    })
  },

  // 减号函数
  bindMinus: function(e) {
    var _index = parseInt(e.currentTarget.dataset.index)
    var _num = this.data.carts[_index].num
    if(_num > 1) {
      _num --
    }
    this.data.carts[_index].num = _num

    this.setData({
      carts: this.data.carts,
    })

    this.sum()
  },
  // 加号函数
  bindPlus:function (e) {
    var _index = parseInt(e.currentTarget.dataset.index)
    var _num = this.data.carts[_index].num
    _num ++

    this.data.carts[_index].num = _num
    this.setData({
      carts: this.data.carts,
    })

    this.sum()
  },

  // 从购物车中移除商品
  removeShopCard:function(e) {
    var _index = parseInt(e.currentTarget.dataset.index);

    var that = this;
    wx.showModal({
      title: '确定删除',
      content: '你真的要删除吗？',
      success:function(res) {
        if(res.confirm) {
          console.log("移除之前：", that.data.carts)
          that.data.carts.splice(_index,1)
          console.log("移除之后：", that.data.carts)

          that.sum();

          that.setData({
            carts: that.data.carts,
          });
        } else {
          return false;
        }
      }
    })
  },

  // 全选按钮
  bindSelectAll:function(e) {
    var _selectedAllStatus = this.data.selectedAllStatus
    _selectedAllStatus = !_selectedAllStatus

    var _carts = this.data.carts
    for(var i=0;i<_carts.length;i++) {
      _carts[i].selected = _selectedAllStatus
    }

    this.setData({
      selectedAllStatus: _selectedAllStatus,
      carts: _carts,
    })

    console.log(_carts)
    this.sum()
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
